/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.server.modules.sys.web.businessrecord;

import com.server.common.config.Global;
import com.server.common.persistence.Page;
import com.server.common.utils.MD5Utils;
import com.server.common.utils.StringUtils;
import com.server.common.web.BaseController;
import com.server.modules.sys.entity.businessrecord.TBusinessRecord;
import com.server.modules.sys.entity.member.Member;
import com.server.modules.sys.service.businessrecord.TBusinessRecordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 商务人员谈单记录表Controller
 * @author 金
 * @version 2017-04-18
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/businessrecord/tBusinessRecord")
public class TBusinessRecordController extends BaseController {

	@Autowired
	private TBusinessRecordService tBusinessRecordService;
	
	@ModelAttribute
	public TBusinessRecord get(@RequestParam(required=false) String id) {
		TBusinessRecord entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tBusinessRecordService.get(id);
		}
		if (entity == null){
			entity = new TBusinessRecord();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:businessrecord:tBusinessRecord:view")
	@RequestMapping(value = {"list", ""})
	public String list(TBusinessRecord tBusinessRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TBusinessRecord> page = tBusinessRecordService.findPage(new Page<TBusinessRecord>(request, response), tBusinessRecord); 
		model.addAttribute("page", page);
		model.addAttribute("tBusinessRecord", tBusinessRecord);
		return "modules/sys/businessrecord/tBusinessRecordList";
	}

	@RequiresPermissions("sys:businessrecord:tBusinessRecord:view")
	@RequestMapping(value = "form")
	public String form(TBusinessRecord tBusinessRecord, Model model) {
		model.addAttribute("tBusinessRecord", tBusinessRecord);
		return "modules/sys/businessrecord/tBusinessRecordForm";
	}

	@RequiresPermissions("sys:businessrecord:tBusinessRecord:edit")
	@RequestMapping(value = "save")
	public String save(TBusinessRecord tBusinessRecord, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tBusinessRecord)){
			return form(tBusinessRecord, model);
		}
		tBusinessRecordService.save(tBusinessRecord);
		addMessage(redirectAttributes, "保存商务谈单记录成功");
		return "redirect:"+Global.getAdminPath()+"/sys/businessrecord/tBusinessRecord/?repage";
	}
	
	@RequiresPermissions("sys:businessrecord:tBusinessRecord:edit")
	@RequestMapping(value = "delete")
	public String delete(TBusinessRecord tBusinessRecord, RedirectAttributes redirectAttributes) {
		tBusinessRecordService.delete(tBusinessRecord);
		addMessage(redirectAttributes, "删除记录成功");
		return "redirect:"+Global.getAdminPath()+"/sys/businessrecord/tBusinessRecord/?repage";
	}
//	@RequiresPermissions("sys:member:member:view")
	@RequestMapping(value = "ajaxSave")
	@ResponseBody
	public Object ajaxSave(TBusinessRecord tBusinessRecord, Model model) throws Exception {
		Member member = tBusinessRecord.getMember();
		if(member==null||StringUtils.isBlank(member.getId())){
			return false;
		}
		String s =(tBusinessRecord.getRecord())+(member.getId())+(tBusinessRecord.getDelFlag());
		String md = MD5Utils.md5Encode(s);
		String w = tBusinessRecordService.checkMd5(md);
		if(tBusinessRecordService.checkMd5(md).equals("0")){
			tBusinessRecord.setMd5(md);
			tBusinessRecordService.save(tBusinessRecord);
			return true;
		}
		else {return false;}
	}
//	@RequiresPermissions("sys:member:member:view")
	@RequestMapping(value = "ajaxDelete")
	@ResponseBody
	public Object ajaxDelete(TBusinessRecord tBusinessRecord) throws  Exception{
		if(tBusinessRecord==null||StringUtils.isBlank(tBusinessRecord.getId())){
			return false;
		}
		String df = tBusinessRecord.getMd5();
		Member member = tBusinessRecord.getMember();
		String s =(tBusinessRecord.getRecord())+(member.getId())+"1";
		String md = MD5Utils.md5Encode(s);
		tBusinessRecord.setMd5(md);
		tBusinessRecord.setDelFlag("1");
		tBusinessRecordService.save(tBusinessRecord);
		return true;
	}

}